﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EIA.PM.DAL;
using EIA.PM.Model;

namespace EIA.PM.BLL
{
    public class XStaffRolesBlp : IXStaffRolesBlp
    {
        IRStaffRolesDao _RStaffRolesDao = null;
        public XStaffRolesBlp()
        {
            _RStaffRolesDao = X.DaoFactory.Create<IRStaffRolesDao>();
        }

        public bool Update(int stf_id, IList<int> role_ids)
        {
            Dictionary<string, object> con = new Dictionary<string, object>();
            con[RStaffRoles.STF_ID] = stf_id;
            _RStaffRolesDao.DASession.BeginTransaction();
            try
            {
                _RStaffRolesDao.Delete(con);
                foreach (var role_id in role_ids)
                {
                    RStaffRoles s_r = new RStaffRoles(stf_id, role_id);
                    _RStaffRolesDao.Insert(s_r);
                }
                _RStaffRolesDao.DASession.CommitTransaction();
                return true;
            }
            catch
            {
                _RStaffRolesDao.DASession.RollbackTransaction();
                return false;
            }
        }

        public bool UpdateMany(IList<int> stf_ids, IList<int> role_ids)
        {
            _RStaffRolesDao.DASession.BeginTransaction();
            try
            {
                foreach (var stf_id in stf_ids)
                {
                    Dictionary<string, object> con = new Dictionary<string, object>();
                    con[RStaffRoles.STF_ID] = stf_id;

                    _RStaffRolesDao.Delete(con);
                    foreach (var role_id in role_ids)
                    {
                        RStaffRoles s_r = new RStaffRoles(stf_id, role_id);
                        _RStaffRolesDao.Insert(s_r);
                    }
                }
                _RStaffRolesDao.DASession.CommitTransaction();
                return true;
            }
            catch
            {
                _RStaffRolesDao.DASession.RollbackTransaction();
                return false;
            }
        }

        public Tuple<int, IList<int>> List(int stf_id)
        {
            Dictionary<string, object> con = new Dictionary<string, object>();
            con[RStaffRoles.STF_ID] = stf_id;
            IList<RStaffRoles> list = _RStaffRolesDao.List(con);
            Tuple<int, IList<int>> tuple = new Tuple<int, IList<int>>(stf_id, new List<int>());
            foreach (var s_r in list)
            {
                tuple.Item2.Add(s_r.RoleId);
            }
            return tuple;
        }
    }
}
